package com.pignest.hotel.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pignest.hotel.beans.User;

public enum Permission {
	
	BOOKING("预订房间", true), 
	RECEPTION("前台接待", true),
	ROOM_STATUS("房间状态", true), 
	MEMBER_MANAGER("会员管理", true), 
	ACCOUNT_MANAGER("账务管理", true), 
	REPORT("报表", true), 
	ADMINISTER("系统管理", true),
		LOG("系统日志", true, 1),
		DUTY("值班记录", true, 1),
		CARD("卡片管理", true, 1),
	SETTING("基础设置", true), 
		PARAMETER("参数设置", true, 1),
		ROOM_TYPE("房间类型管理", true, 1),
		ROOM_MANAGER("房间管理", true, 1),
		ROLE("角色管理", true, 1),
		USER_MANAGER("用户管理", true, 1),
		GOODS("物品管理", true, 1),
		MEMBER_LEVEL("会员等级", true, 1),
		MEMBER_PRICE("会员房价", true, 1),
		CONSULTATIVE("协议单位", true, 1),
	SELF_SERVICE("自助服务", true), 
		
	DENY(null, false) {
		@Override
		public boolean alow(User user) {
			return false;
		}
	}, 
	SUPER(null, false) {
		@Override
		public boolean alow(User user) {
			return user != null && user.isSuperUser();
		}
	}, 
	LOGIN(null, false) {
		@Override
		public boolean alow(User user) {
			return user != null;
		}
	}
	;
	
	//显示名
	private final String displayName;
	private final boolean open;
	private final int level;
	
	private Permission(String cname, boolean open) {
		this(cname, open, 0);
	}
	private Permission(String cname, boolean open, int level) {
		this.displayName = cname;
		this.open = open;
		this.level = level;
	}
	
	public String getName() {
		return name();
	}
	
	public String getDisplayName() {
		return displayName;
	}
	
	public int getLevel() {
		return level;
	}
	
	public boolean isOpen() {
		return open;
	}
	
	public boolean alow(User user) {
		if (Hotel.TEST)
			return true;
		if (user == null)
			return false;
		if (user.isSuperUser())
			return true;
		Collection<String> permissions = user.getPermissions();
		for (String permission : permissions) {
			if (this.name().equals(permission))
				return true;
		}
		return false;
	}
	
	/////////////////////////////////////////////////////////////////////////////
	
	public static String[][] getPermissions() {
		Permission[] ps = Permission.values();
		List<String[]> r = new ArrayList<String[]>();
//		String[][] r = new String[ps.length][2];
		for (int i = 0; i < ps.length; i++) {
			Permission p = ps[i];
			if (p.open)
				r.add(new String[] {p.name(), p.displayName});
		}
		return r.toArray(new String[2][r.size()]);
	}
	
	/** non-exception version of {@link #valueOf(String)} */
	public static Permission valueof(String name) {
		try {
			return Permission.valueOf(name);
		} catch (RuntimeException e) {
		}
		return null;
	}
	
	public static boolean alow(String permissions, User user) {
		if (StringUtils.isEmpty(permissions))
			return true;
		String[] ps = StringUtils.split(permissions, ',');
		return alow(ps, user);
	}
	
	public static boolean alow(String[] ps, User user) {
		for (String permission : ps) {
			Permission p = valueof(permission);
			if (p != null && !p.alow(user)) {
				return false;
			}
			//所有人不被不存在的权限所限制
		}
		return true;
	}
}
